const DB = require('../../utils/db');

module.exports = {
	list: async () => {
		let [list] = await DB.query('select id as value, name as label from t_role ');
		return list;
	},
	listAll: async (keywords) => {
		let sql = "select id, name, routes, date_format(create_time,'%Y-%m-%d %H:%i:%s') createTime from t_role";
		if (keywords) {
			sql = sql + ` where name like '%${keywords}%'`;
		}
		let [list] = await DB.query(sql);
		return list;
	},
	add: async (name, routes, halfCheckedKeys, checkedKeys) => {
		await DB.query('insert into t_role (name, routes,half_checked_keys, checked_keys) values(?,?,?,?)', [name, routes, halfCheckedKeys, checkedKeys]);
	},
	edit: async (name, routes, halfCheckedKeys, checkedKeys, id) => {
		await DB.query(`update t_role set name=?, routes=?, half_checked_keys=?, checked_keys=? where id =?`, [name, routes, halfCheckedKeys, checkedKeys, id]);
	},
	del: async (id) => {
		await DB.query('delete from t_role where id=?', [id]);
	},
	detail: async (id) => {
		let [list] = await DB.query('select id,name, routes, half_checked_keys halfCheckedKeys, checked_keys checkedKeys from t_role where id=?', [id]);
		if (!list.length) throw new Error('角色ID有误');
		return list[0];
	}
};
